﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace EFong.Contract
{
    [DataContract]
    public class HotelBookingDetail
    {
        private Guid _id = Guid.NewGuid();
        [DataMember]
        public Guid ID
        {
            get { return _id; }
            set { _id = value; }
        }

        private HotelBooking _hotelBooking;
        public HotelBooking HotelBooking
        {
            get { return _hotelBooking; }
            set { _hotelBooking = value; }
        }

        private string _hotelLeave = string.Empty;
        [DataMember]
        public string HotelLeave
        {
            get { return _hotelLeave; }
            set { _hotelLeave = value; }
        }

        private string _hotelName = string.Empty;
        [DataMember]
        public string HotelName
        {
            get { return _hotelName; }
            set { _hotelName = value; }
        }

        private string _guestName = string.Empty;
        [DataMember]
        public string GuestName
        {
            get { return _guestName; }
            set { _guestName = value; }
        }

        private DateTime? _checkInDate;
        [DataMember]
        public DateTime? CheckInDate
        {
            get { return _checkInDate; }
            set { _checkInDate = value; }
        }

        private DateTime? _checkOutDate;
        [DataMember]
        public DateTime? CheckOutDate
        {
            get { return _checkOutDate; }
            set { _checkOutDate = value; }
        }

        //private int _days;
        //[DataMember]
        public int Days
        {
            get
            {
                if (CheckOutDate == null || CheckOutDate == null)
                    return 0;

                DateTime checkOutDate = new DateTime(CheckOutDate.Value.Year, CheckOutDate.Value.Month, CheckOutDate.Value.Day);
                DateTime checkInDate = new DateTime(CheckInDate.Value.Year, CheckInDate.Value.Month, CheckInDate.Value.Day);

                return (new TimeSpan(checkOutDate.Ticks - checkInDate.Ticks)).Days;
            }
            //get { return _days; }
            //set { _days = value; }
        }

        private int _rooms;
        [DataMember]
        public int Rooms
        {
            get { return _rooms; }
            set { _rooms = value; }
        }

        private string _roomType = string.Empty;
        [DataMember]
        public string RoomType
        {
            get { return _roomType; }
            set { _roomType = value; }
        }

        private decimal _price;
        [DataMember]
        public decimal Price
        {
            get { return _price; }
            set { _price = value; }
        }

        private decimal? _vendorPrice;
        [DataMember]
        public decimal? VendorPrice
        {
            get { return _vendorPrice; }
            set { _vendorPrice = value; }
        }

        private decimal? _vendorDiscount;
        [DataMember]
        public decimal? VendorDiscount
        {
            get { return _vendorDiscount; }
            set { _vendorDiscount = value; }
        }

        private decimal? _hotelTax;
        [DataMember]
        public decimal? HotelTax
        {
            get { return _hotelTax; }
            set { _hotelTax = value; }
        }

        //1)	预付
        //a)	有”返点”的时候
        //(客户)应收 = 单价 * 数量 * 晚数
        //(供应商)应付 = 应收 * (1 - 返点) 
        //利润 = (客户)应收 – (供应商)应付
        //b)	无”返点”的时候
        //(客户)应收 = 单价 * 数量 * 晚数
        //(供应商)应付 = 底价 * 数量 * 晚数
        //利润 = (客户)应收 – (供应商)应付
        //2)	现付
        //a)	一定有”返点”
        //(客户)应收 =  0
        //(供应商)应收 = 利润 = 单价 * 返点 * (1 – 酒店扣税) * 数量 * 晚数

        //酒店扣税是现付才有的

        //private decimal _totalPrice;
        //[DataMember]
        public decimal? TotalPrice
        {
            get
            {
                return Price * Rooms * Days;

                //if (HotelBooking == null)
                //    return null;

                //switch (HotelBooking.CustomerPaymentType)
                //{
                //    case "预付":
                //        return Price * Rooms * Days;
                //    case "现付":
                //        return 0;
                //    default:
                //        return null;
                //}
            }
            //get { return _totalPrice; }
            //set { _totalPrice = value; }
        }

        public decimal? TotalVendorPrice
        {
            get
            {
                if (HotelBooking == null)
                    return null;

                switch (HotelBooking.CustomerPaymentType)
                {
                    case "预付":
                        if (VendorDiscount == null)
                        {
                            if (VendorPrice == null)
                                return null;
                            return VendorPrice.Value * Rooms * Days;
                        }
                        else
                            return Price * (1 - VendorDiscount.Value/100) * Rooms * Days;
                    case "现付":
                        if (VendorDiscount == null)
                        {
                            if (HotelTax == null)
                                return null;
                            if (VendorPrice == null)
                                return null;
                            return VendorPrice.Value * (1 + HotelTax.Value/100) * Rooms * Days;
                        }
                        else
                        {
                            if (HotelTax == null)
                                return null;
                            return Price * (1 - VendorDiscount.Value / 100 + HotelTax.Value / 100)  * Rooms * Days;
                        }
                    default:
                        return null;
                }
            }
        }

        //private decimal _profit;
        //[DataMember]
        public decimal? Profit
        {
            get
            {
                if (HotelBooking == null)
                    return null;

                return TotalPrice - TotalVendorPrice * HotelBooking.ExchangeRate;

                //switch (HotelBooking.CustomerPaymentType)
                //{
                //    case "预付":
                //        if (TotalPrice == null)
                //            return null;
                //        if(TotalVendorPrice == null)
                //            return null;
                //        return TotalPrice.Value - TotalVendorPrice.Value;
                //    case "现付":
                //        if (VendorDiscount == null)
                //            return null;
                //        else
                //            return 0 - TotalVendorPrice.Value;
                //    default:
                //        return null;
                //}
            }
            //get { return _profit; }
            //set { _profit = value; }
        }

        private string _hotelOrderRef = string.Empty;
        [DataMember]
        public string HotelOrderRef
        {
            get { return _hotelOrderRef; }
            set { _hotelOrderRef = value; }
        }

        private string _hotelConfirmationRef = string.Empty;
        [DataMember]
        public string HotelConfirmationRef
        {
            get { return _hotelConfirmationRef; }
            set { _hotelConfirmationRef = value; }
        }

        public decimal? CulcIntergral(Customer customer) 
        {
            if (customer == null)
                return null;
            if (customer.HotelIntergralList.Count != 0)
            {
                foreach (var customerHotelIntergral in customer.HotelIntergralList) 
                {
                    if (customerHotelIntergral.HotelLevel.Name == this.HotelLeave)
                        return customerHotelIntergral.Intergral;
                }

                return null;
            }
            else 
            {
                if (TotalPrice == null)
                    return null;
                if (customer.HotelIntergralRate == null)
                    return null;
                return TotalPrice.Value * customer.HotelIntergralRate.Value;
            }
        }

        private decimal _intergral;
        [DataMember]
        public decimal Intergral
        {
            get { return _intergral; }
            set { _intergral = value; }
        }

        private string _入住日期 = null;
        public string 入住日期
        {
            get
            {
                if (_入住日期 == null)
                    if(this.CheckInDate == null)
                        return string.Empty;
                    else
                        return this.CheckInDate.Value.ToString("yyyy/MM/dd");
                else
                    return _入住日期;
            }
            set
            {
                _入住日期 = value;
            }
        }

        private string _离店日期 = null;
        public string 离店日期
        {
            get
            {
                if (_离店日期 == null)
                    if (this.CheckOutDate == null)
                        return string.Empty;
                    else
                        return this.CheckOutDate.Value.ToString("yyyy/MM/dd");
                else
                    return _离店日期;
            }
            set
            {
                _离店日期 = value;
            }
        }

        public override bool Equals(object obj)
        {
            HotelBookingDetail temp = obj as HotelBookingDetail;
            if (temp == null)
                return false;
            return (this.ID.Equals(temp.ID));
        }
    }
}

